home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
gnu
/
sed
/
sed113.zoo
/
sed.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-01
|
40KB
|
1,829 lines
/* GNU SED, a batch stream editor.
Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef __STDC__
#define VOID void
#else
#define VOID char
#endif
#define _GNU_SOURCE
#include <ctype.h>
#ifndef isblank
#define isblank(c) ((c) == ' ' || (c) == '\t')
#endif
#include <stdio.h>
#include <sys/types.h>
#include "regex.h"
#include "getopt.h"
#if defined(STDC_HEADERS)
#include <stdlib.h>
#endif
#if defined(USG) || defined(STDC_HEADERS)
#include <string.h>
#if !defined(STDC_HEADERS)
#include <memory.h>
#endif
#else
#include <strings.h>
#endif
/*
GCC 2.3.1 PL1 with MiNTlibs PL25 doesn't seem to like any of the
following two choices, so when you compile this source on an Atari
they both are ifdef'ed out. If I interpret the source and include-
files correctly it doesn't affect the finished executable. However,
a mistake or two is always easy to make. :-)
-Daniel Eriksson
*/
#ifndef atarist
#ifndef HAVE_BCOPY
#ifdef HAVE_MEMCPY
#define bcopy(FROM,TO,LEN) memcpy(TO,FROM,LEN,sizseof(char))
#else
void
bcopy (from, to, len)
char *from;
char *to;
int len;
{
if (from < to)
{
from += len - 1;
to += len - 1;
while (len--)
*to-- = *from--;
}
else
while (len--)
*to++ = *from++;
}
#endif
#endif
#endif /* atarist */
char *version_string = "GNU sed version 1.13 Atari";
/* Struct vector is used to describe a chunk of a sed program. There is one
vector for the main program, and one for each { } pair. */
struct vector
{
struct sed_cmd *v;
int v_length;
int v_allocated;
struct vector *return_v;
int return_i;
};
/* Goto structure is used to hold both GOTO's and labels. There are two
separate lists, one of goto's, called 'jumps', and one of labels, called
'labels'.
the V element points to the descriptor for the program-chunk in which the
goto was encountered.
the v_index element counts which element of the vector actually IS the
goto/label. The first element of the vector is zero.
the NAME element is the null-terminated name of the label.
next is the next goto/label in the list. */
struct sed_label
{
struct vector *v;
int v_index;
char *name;
struct sed_label *next;
};
/* ADDR_TYPE is zero for a null address,
one if addr_number is valid, or
two if addr_regex is valid,
three, if the address is '$'
Other values are undefined.
*/
#define ADDR_NULL 0
#define ADDR_NUM 1
#define ADDR_REGEX 2
#define ADDR_LAST 3
struct addr
{
int addr_type;
struct re_pattern_buffer *addr_regex;
int addr_number;
};
/* Aflags: If the low order bit is set, a1 has been
matched; apply this command until a2 matches.
If the next bit is set, apply this command to all
lines that DON'T match the address(es).
*/
#define A1_MATCHED_BIT 01
#define ADDR_BANG_BIT 02
struct sed_cmd
{
struct addr a1, a2;
int aflags;
char cmd;
union
{
/* This structure is used for a, i, and c commands */
struct
{
char *text;
int text_len;
}
cmd_txt;
/* This is used for b and t commands */
struct sed_cmd *label;
/* This for r and w commands */
FILE *io_file;
/* This for the hairy s command */
/* For the flags var:
low order bit means the 'g' option was given,
next bit means the 'p' option was given,
and the next bit means a 'w' option was given,
and wio_file contains the file to write to. */
#define S_GLOBAL_BIT 01
#define S_PRINT_BIT 02
#define S_WRITE_BIT 04
#define S_NUM_BIT 010
struct
{
struct re_pattern_buffer *regx;
char *replacement;
int replace_length;
int flags;
int numb;
FILE *wio_file;
}
cmd_regex;
/* This for the y command */
unsigned char *translate;
/* For { */
struct vector *sub;
/* for t and b */
struct sed_label *jump;
} x;
};
/* Sed operates a line at a time. */
struct line
{
char *text; /* Pointer to line allocated by malloc. */
int length; /* Length of text. */
int alloc; /* Allocated space for text. */
};
/* This structure holds information about files opend by the 'r', 'w',
and 's///w' commands. In paticular, it holds the FILE pointer to
use, the file's name, a flag that is non-zero if the file is being
read instead of written. */
#define NUM_FPS 32
struct
{
FILE *phile;
char *name;
int readit;
}
file_ptrs[NUM_FPS];
#if defined(__STDC__)
# define P_(s) s
#else
# define P_(s) ()
#endif
void close_files ();
void panic P_ ((char *str,...));
char *__fp_name P_ ((FILE * fp));
FILE *ck_fopen P_ ((char *name, char *mode));
void ck_fwrite P_ ((char *ptr, int size, int nmemb, FILE * stream));
void ck_fclose P_ ((FILE * stream));
VOID *ck_malloc P_ ((int size));
VOID *ck_realloc P_ ((VOID * ptr, int size));
char *ck_strdup P_ ((char *str));
VOID *init_buffer P_ ((void));
void flush_buffer P_ ((VOID * bb));
int size_buffer P_ ((VOID * b));
void add_buffer P_ ((VOID * bb, char *p, int n));
void add1_buffer P_ ((VOID * bb, int ch));
char *get_buffer P_ ((VOID * bb));
void compile_string P_ ((char *str));
void compile_file P_ ((char *str));
struct vector *compile_program P_ ((struct vector * vector));
void bad_prog P_ ((char *why));
int inchar P_ ((void));
void savchar P_ ((int ch));
int compile_address P_ ((struct addr * addr));
void compile_regex P_ ((int slash));
struct sed_label *setup_jump P_ ((struct sed_label * list, struct sed_cmd * cmd, struct vector * vec));
FILE *compile_filename P_ ((int readit));
void read_file P_ ((char *name));
void execute_program P_ ((struct vector * vec));
int match_address P_ ((struct addr * addr));
int read_pattern_space P_ ((void));
void append_pattern_space P_ ((void));
void line_copy P_ ((struct line * from, struct line * to));
void line_append P_ ((struct line * from, struct line * to));
void str_append P_ ((struct line * to, char *string, int length));
void usage P_ ((void));
extern char *myname;
/* If set, don't write out the line unless explictly told to */
int no_default_output = 0;
/* Current input line # */
int input_line_number = 0;
/* Are we on the last input file? */
int last_input_file = 0;
/* Have we hit EOF on the last input file? This is used to decide if we
have hit the '$' address yet. */
int input_EOF = 0;
/* non-zero if a quit command has been executed. */
int quit_cmd = 0;
/* Have we done any replacements lately? This is used by the 't' command. */
int replaced = 0;
/* How many '{'s are we executing at the moment */
int program_depth = 0;
/* The complete compiled SED program that we are going to run */
struct vector *the_program = 0;
/* information about labels and jumps-to-labels. This is used to do
the required backpatching after we have compiled all the scripts. */
struct sed_label *jumps = 0;
struct sed_label *labels = 0;
/* The 'current' input line. */
struct line line;
/* An input line that's been stored by later use by the program */
struct line hold;
/* A 'line' to append to the current line when it comes time to write it out */
struct line append;
/* When we're reading a script command from a string, 'prog_start' and
'prog_end' point to the beginning and end of the string. This
would allow us to compile script strings that contain nulls, except
that script strings are only read from the command line, which is
null-terminated */
unsigned char *prog_start;
unsigned char *prog_end;
/* When we're reading a script command fr